<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Topics extends Model
{
    protected $table = 'topics';

    protected $guarded = ['id', '_token'];

    public function category()
    {
        return $this->belongsTo('App\Models\Category', 'category_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id', 'id');
    }

    public function community()
    {
        return $this->belongsTo('App\Models\Communities', 'community_id', 'id');
    }

    public function replies()
    {
        return $this->hasMany('App\Models\Replies', 'topic_id', 'id');
    }

    public function scopeOrder($query, $order)
    {
        $query->orderBy('order');
        if ($order == 'recent') {
            return $query->orderBy('created_at', 'desc');
        } else {
            $query->orderBy('updated_at', 'desc');
        }
        return $query->orderBy('reply_count', 'desc')->orderBy('view_count', 'desc');
    }
}
